﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Diagnostics;

namespace CloudStorageLight.Core.Tests
{
    [TestClass]
    public class FileBlobAdapterTest
    {
        [TestMethod]
        public void コンストラクタ()
        {
            using (var adapt = CreateAdapter("UnitTest0"))
            {
            }
        }

        private BlobAdapterBase CreateAdapter(string workFolder)
        {
            if (Directory.Exists(workFolder)) Directory.Delete(workFolder, true);
            var direInfo = Directory.CreateDirectory(workFolder);
            return new FileBlobAdapter("/LOCALFILE/", direInfo.FullName + "\\");
        }

        [TestMethod]
        public void アップロード_取得_削除()
        {
            using (var adapt = CreateAdapter("UnitTest1"))
            {
                var item = new BlobItem(null, "/LOCALFILE/Work.txt");
                using (var st = new MemoryStream(new byte[] { 32 }))
                {
                    adapt.UploadAsync(item, st).Wait();
                }
                using (var st = new MemoryStream())
                {
                    adapt.DownloadAsync(item, st).Wait();
                    Assert.AreEqual(32, st.ToArray()[0]);
                }
                var list = adapt.GetFilesAsync("/LOCALFILE/", 100).Result;
                Assert.AreEqual(2, list.Count);

                adapt.DeleteAsync(item).Wait();

                Assert.IsFalse(adapt.ExistsAsync(item.Path).Result);

                list = adapt.GetFilesAsync("/LOCALFILE/", 100).Result;
                Assert.AreEqual(1, list.Count);

            }
        }

        [TestMethod]
        public void フォルダ作成・コピー・削除()
        {
            using (var adapt = CreateAdapter("UnitTest2"))
            {
                var dire = new BlobDirectory(null, "/LOCALFILE/Temp/");
                var item = new BlobFile(null, "/LOCALFILE/Temp/Work.txt");

                try
                {
                    adapt.DeleteAsync(item).Wait();
                }
                catch { };
                try
                {
                    adapt.DeleteAsync(dire).Wait();
                }
                catch { };

                adapt.CreateDirectoryAsync(dire).Wait();

                var list = adapt.GetFilesAsync("/LOCALFILE/Temp/", 100).Result;
                Assert.AreEqual(1, list.Count);

                using (var st = new MemoryStream(new byte[] { 32 }))
                {
                    adapt.UploadAsync(item, st).Wait();
                }
                Assert.IsTrue(adapt.ExistsAsync(item.Path).Result);

                list = adapt.GetFilesAsync("/LOCALFILE/Temp/", 100).Result;
                Assert.AreEqual(2, list.Count);

                adapt.CopyAsync("/LOCALFILE/Temp/Work2.txt", item).Wait();

                list = adapt.GetFilesAsync("/LOCALFILE/Temp/", 100).Result;
                Assert.AreEqual(3, list.Count);

                try
                {
                    adapt.DeleteAsync(dire).Wait();
                    Assert.Fail();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }

                adapt.DeleteAsync(item).Wait();
                var item2 = new BlobFile(null, "/LOCALFILE/Temp/Work2.txt");
                adapt.DeleteAsync(item2).Wait();
                adapt.DeleteAsync(dire).Wait();
                Assert.IsFalse(adapt.ExistsAsync(dire.Path).Result);

            }
        }

    }
}
